package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.security.annotation.Inner;
import com.workcell.workdata.xthings.dto.BindInfoDto;
import com.workcell.workdata.xthings.dto.DeviceBindDto;
import com.workcell.workdata.xthings.dto.DeviceReplaceVo;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.DeviceBind;
import com.workcell.workdata.xthings.service.DeviceBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Huang Zengtian
 */
@Api(value = "deviceBind", tags = "设备绑定")
@RestController
@RequestMapping("/deviceBind")
@RequiredArgsConstructor
public class DeviceBindController {
    private final DeviceBindService deviceBindService;

    @ApiOperation("查看当前设备绑定列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "设备编码", name = "deviceNo"),
            @ApiImplicitParam(value = "设备类型", name = "hardwareType")
    })
    @GetMapping("/bindList")
    public R list(String deviceNo, String hardwareType) {
        Assert.notEmpty(deviceNo, "设备编码不能为空");
        Assert.notEmpty(hardwareType, "设备类型编码不能为空");
        return R.ok(deviceBindService.bindList2(deviceNo, hardwareType));
    }

    @ApiOperation("根据短链码查询设备绑定")
    @GetMapping("/bindList2")
    public R list(String chainNum) {
        Assert.notEmpty(chainNum, "短链码不能为空");
        return R.ok(deviceBindService.bindList2(chainNum));
    }

    @ApiOperation("设备绑定")
    @PostMapping("/bind")
    public R bind(@Valid @RequestBody DeviceBind deviceBind) {
        return R.ok(deviceBindService.bind(deviceBind));
    }

    @ApiOperation("设备解绑")
    @PostMapping("/unbind")
    public R unbind(@Valid @RequestBody DeviceBind deviceBind) {
        return R.ok(deviceBindService.unbind(deviceBind));
    }

    @ApiOperation("设备更换")
    @PostMapping("/replace")
    public R replace(@Valid @RequestBody DeviceReplaceVo deviceReplaceVo) {
        return R.ok(deviceBindService.replace(deviceReplaceVo));
    }

    @Inner
    @ApiOperation("获取设备信息")
    @GetMapping("/getBindInfo")
    public R<BindInfoDto> getBindInfo(String masterDeviceNo, String addressId) {
        BindInfoDto bindInfo = deviceBindService.getBindInfo(masterDeviceNo, addressId);
        return R.ok(bindInfo);
    }

    @Inner
    @ApiOperation("获取所有设备绑定列表")
    @GetMapping("/allBindList")
    public R<List<DeviceBindDto>> allBindList() {
        List<DeviceBindDto> deviceBindDtoList = deviceBindService.allBindList();
        return R.ok(deviceBindDtoList);
    }

	@Inner
	@ApiOperation("获取设备信息")
	@GetMapping("/getBindInfoByDeviceNo")
	public R<DeviceBindDto> getBindInfoByDeviceNo(String deviceNo,String queryType) {
		DeviceBindDto bindInfo = deviceBindService.getBindInfoByDeviceNo(deviceNo,queryType);
		return R.ok(bindInfo);
	}

	@Inner
	@GetMapping("/bindListInner")
	public R<List<CodeChainnum>> bindListInner(String deviceNo, String hardwareType) {
		return R.ok(deviceBindService.bindList2(deviceNo, hardwareType));
	}

}
